
#    This file is part of pyCorreios.
#    
#    pyCorreios is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    
#    pyCorreios is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with pyCorreios.  If not, see <http://www.gnu.org/licenses/>.


#
# IMPORTS
#
import os
import tracking

#
# CONSTANTS
#
CONFIG_DIR=os.path.join(os.getenv('HOME', '/tmp'), ".pycorreios", "tracking") 

#
# CODE
#
class Data(object):
    def __init__(self):
        self.__createDirectories(CONFIG_DIR)
        self.__data = self.__scanForDeliveries()
    # __init__()
    
    def update(self):
        self.__updateDeliveries(self.__data)
        self.__saveDeliveries(self.__data)
    # update()
        
    def add(self, code):
        self.__data[code] = tracking.retrieveNacional(code)
        self.__saveDeliveries(self.__data)
    # add()
    
    def remove(self, code):
        del(self.__data[code])
        if os.path.isdir(CONFIG_DIR):
            if os.path.isfile(CONFIG_DIR + "/" + code):
                os.remove(CONFIG_DIR + "/" + code)
    # remove()
    
    def getCodes(self):
        return self.__data.keys()
    # getCodes()
    
    def get(self, code):
        return self.__data.get(code, [])
    # get()
    
    def __updateDeliveries(self, dict):
        for (code, data) in dict.items():
            new_data = tracking.retrieveNacional(code)
            if new_data != None:
                 dict[code] = new_data               
    
    def __saveDeliveries(self, dict):
        if os.path.isdir(CONFIG_DIR):
            for (code, data) in dict.items():
                try:
                    fp = open(CONFIG_DIR + "/" + code, 'w')
                    for line in data:
                        fp.write('|'.join(line) + '\n')
                    fp.close()
                except:
                    pass
    
    def __scanForDeliveries(self):
        if os.path.isdir(CONFIG_DIR):
            codes = os.listdir(CONFIG_DIR)
            dict = {}
            for code in codes:
                data = []
                try:
                    fp = open(CONFIG_DIR + "/" + code, 'r')
                    for line in fp:
                        fields = line.split('|')
                        data.append(fields)
                    fp.close()
                except:
                    pass
                dict[code] = data
            return dict
        else:
            return {}
    # __scaForDeliveries()
    
    def __createDirectories(self, path):
        
        if path == "" or os.path.isdir(path):
            return 
        
        head, tail = os.path.split(path)
        
        self.__createDirectories(head)
        
        if tail != "":
            os.mkdir(path)        
    # __createDirectories()
# Data
